package com.ruicar.afs.cloud.basic.reprint.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.basic.api.reappear.*;
import com.ruicar.afs.cloud.basic.common.entity.*;
import com.ruicar.afs.cloud.basic.common.service.*;
import com.ruicar.afs.cloud.basic.reprint.util.ContractUtils;
import com.ruicar.afs.cloud.basic.sync.ApplyLoanDataMessage;
import com.ruicar.afs.cloud.basic.sync.ApplyLoanDataProcessor;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.*;
import com.ruicar.afs.cloud.common.util.EmptyUtils;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;

/**
 * @Author chuke
 * @create 2020/7/8 22:20
 */
@Slf4j
@RestController
@RequestMapping("/reprint")
@AllArgsConstructor
public class ReprintController {
    private final BasicAffiliatedUnitService unitService;
    private final ApplyLoanDataProcessor applyLoanDataProcessor;
    private final BasicCarDetailsService basicCarDetailsService;
    private final BasicCustBaseInfoService basicCustBaseInfoService;
    private final BasicContractCustRelationService basicContractCustRelationService;
    private final BasicFinancialAgreementService basicFinancialAgreementService;
    private final BasicCustAddressDetailsService basicCustAddressDetailsService;
    private final BasicBankCardService basicBankCardService;
    private final BasicAffiliatedUnitService basicAffiliatedUnitService;
    private final BasicChannelInfoService basicChannelInfoService;
    private final BasicMainInfoService basicMainInfoService;
    private final BasicProductParamService basicProductParamService;
    private final BasicFinancingItemsService basicFinancingItemsService;

    @ApiOperation("查询合同重出挂靠信息")
    @PostMapping("/findAffiliatedInfo")
    public IResponse findAffiliatedInfo(@RequestBody BasicAffiliatedUnit unit) {
        BasicAffiliatedUnit affiliate = unitService.getOne(Wrappers.<BasicAffiliatedUnit>lambdaQuery()
                .eq(BasicAffiliatedUnit::getContractNo, unit.getContractNo()));
        return new IResponse().setData(affiliate);
    }

    @ApiOperation("查询合同重出挂靠信息")
    @PostMapping("/contractTest")
    public IResponse contractTest(@RequestBody ApplyLoanDataMessage applyLoanDataMessage) {
        try {
            applyLoanDataProcessor.processMessage(applyLoanDataMessage);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return IResponse.fail(e.getMessage());
        }
        return new IResponse().setData("成功");
    }

    //    @ApiOperation("生成模板")
//    @PostMapping("/contractPrint")
//    public IResponse contractPrint(@RequestBody ApplyLoanDataMessage applyLoanDataMessage) {
//        ComPrintFormManage getComPrintFormManage = comPrintFormManageService.getOne(Wrappers.<ComPrintFormManage>query().lambda()
//                .eq(ComPrintFormManage::getPrintFormName, comPrintFormManage.getPrintFormName())
//                .eq(ComPrintFormManage::getVersionId, comPrintFormManage.getVersionId())
//        );
//        comPrintFormManages.add(getComPrintFormManage);
//        IResponse<com.ruicar.afs.cloud.manage.api.dto.DowdTemVo> dowdTemVoIResponse = basicMainInfoFeign.ContractQuery(vo.getContractNo());
//        com.ruicar.afs.cloud.manage.api.dto.DowdTemVo dowdTemVo = dowdTemVoIResponse.getData();
//        // 生成模板
//        JSONObject jsonTemp = (JSONObject) JSONObject.toJSON(dowdTemVo);
//        comPrintFormManageService.print(jsonTemp, String.valueOf(getComPrintFormManage.getId()), vo.getCaseNo(), String.valueOf(getComPrintFormManage.getId()));
//
//    }
    //查询合同信息
    @ApiOperation("查询合同信息")
    @PostMapping("/ContractQuery")
    public IResponse ContractQuery(@RequestParam(value = "contractNo") String contractNo) {
        //总表
        DowdTemVo dowdTemVo = new DowdTemVo();
        //挂靠信息
        BasicAffiliatedUnitVo basicAffiliatedUnitVo = new BasicAffiliatedUnitVo();


        BasicMainInfoVo basicMainInfoVoEnd = new BasicMainInfoVo();

        BasicMainInfoVo basicMainInfoVoStart = new BasicMainInfoVo();
        //订单信息
        BasicCustBaseInfoVo basicCustBaseInfoVo = new BasicCustBaseInfoVo();
        //合同信息
        BasicMainInfoVo basicMainInfoVo = new BasicMainInfoVo();
        //主借人
        BasicMainCustVo mainBasicMainCustVo = new BasicMainCustVo();
        //共借人
        BasicMainCustVo coBasicMainCustVo = new BasicMainCustVo();
        //保证人
        BasicMainCustVo bailBasicMainCustVo = new BasicMainCustVo();
        //车款融资信息
        BasicFinancialAgreementVo carBasicFinancialAgreementVo = new BasicFinancialAgreementVo();
        //附加贷融资信息
        BasicFinancialAgreementVo addBasicFinancialAgreementVo = new BasicFinancialAgreementVo();
        //渠道信息
        BasicChannelInfoVo basicChannelInfoVo = new BasicChannelInfoVo();
        //车辆信息
        BasicCarDetailsVo carDetailsVo = new BasicCarDetailsVo();
        //银行卡信息
        BasicBankCardVo basicBankCardVo = new BasicBankCardVo();

        String isPurchaseTax = "□";
        String isInsurance = "□";
        String isGps = "□";
        String isDecorate = "□";
        String isBoutique = "□";
        String noBoutique = "■";
        String noPurchaseTax = "■";
        String noInsurance = "■";
        String noGps = "■";
        String noDecorate = "■";
        String financeItemNames = "";
        BigDecimal gpsPrice = BigDecimal.ZERO;
        BigDecimal purchaseTaxAmt = BigDecimal.ZERO;
        BigDecimal insuranceAmt = BigDecimal.ZERO;
        BigDecimal gpsAmt = BigDecimal.ZERO;
        BigDecimal decorateAmt = BigDecimal.ZERO;
        BigDecimal boutiqueAmt = BigDecimal.ZERO;
        //增融项信息
        BasicFinancingItemsVo addPriceItems = new BasicFinancingItemsVo();


        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>query().lambda()
                .eq(BasicMainInfo::getContractNo, contractNo)
        );
        if (basicMainInfo != null) {
            basicMainInfoVoStart.setTime(basicMainInfo.getStartDate());
            Calendar calendar = new GregorianCalendar();
            calendar.add(calendar.DATE, 90);
            Date endTime = calendar.getTime();
//            long time = basicMainInfo.getEndDate().getTime() +90*24*60*60*1000;
//            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            basicMainInfoVoEnd.setTime(endTime);
            basicMainInfoVo.setBusinessType(basicMainInfo.getBusinessType());
        }
        basicMainInfoVo.setContractNo(contractNo);
        dowdTemVo.setContract(basicMainInfoVo);
        dowdTemVo.setStart(basicMainInfoVoStart);
        dowdTemVo.setEnd(basicMainInfoVoEnd);
        //查询车辆信息
        BasicCarDetails basicCarDetails = basicCarDetailsService.getOne(Wrappers.<BasicCarDetails>query().lambda()
                .eq(BasicCarDetails::getContractNo, contractNo)
        );
        if (basicCarDetails != null) {
            carDetailsVo.setBrandName(basicCarDetails.getBrandName());
            carDetailsVo.setCarColor(basicCarDetails.getCarColor());
            carDetailsVo.setCarUse(basicCarDetails.getCarUse());
            carDetailsVo.setCarVin(basicCarDetails.getCarVin());
            carDetailsVo.setEngineNo(basicCarDetails.getEngineNo());
            carDetailsVo.setFullModelNum(basicCarDetails.getFullModelNum());
            carDetailsVo.setLicensePlate(basicCarDetails.getLicensePlate());
            carDetailsVo.setModelName(basicCarDetails.getModelName());
            carDetailsVo.setSalePrice(basicCarDetails.getSalePrice());
            mainBasicMainCustVo.setDrivingLicenceNo(basicCarDetails.getDrivingLicenceNo());
        }
        dowdTemVo.setCarDetails(carDetailsVo);
        //查询主借人信息
        BasicContractCustRelation mainBasicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>query().lambda()
                .eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.MAIN_PERSON));
        if (mainBasicContractCustRelation != null) {
            BasicCustBaseInfo mainbasicCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>query().lambda()
                    .eq(BasicCustBaseInfo::getCustNo, mainBasicContractCustRelation.getCustNo())
            );
            BasicCustAddressDetails mainBasicCustAddressDetailsLiving = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>query().lambda()
                    .eq(BasicCustAddressDetails::getCustNo, mainBasicContractCustRelation.getCustNo())
                    .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.LIVE)
            );
            BasicCustAddressDetails mainBasicCustAddressDetailsWork = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>query().lambda()
                    .eq(BasicCustAddressDetails::getCustNo, mainBasicContractCustRelation.getCustNo())
                    .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.WORK)
            );

            if (mainbasicCustBaseInfo != null) {
                mainBasicMainCustVo.setCustName(mainbasicCustBaseInfo.getCustName());
                mainBasicMainCustVo.setCertNo(mainbasicCustBaseInfo.getCertNo());
                mainBasicMainCustVo.setTelPhone(mainbasicCustBaseInfo.getPhone());
            }
            if (mainBasicCustAddressDetailsLiving != null) {
                mainBasicMainCustVo.setLivingAddress(mainBasicCustAddressDetailsLiving.getAddress());
            }
            if (mainBasicCustAddressDetailsWork != null) {
                mainBasicMainCustVo.setMailingAddress(mainBasicCustAddressDetailsWork.getAddress());
            }

        }
        dowdTemVo.setMainCust(mainBasicMainCustVo);
        //查询共借人信息
        BasicContractCustRelation coBasicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>query().lambda()
                .eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.JOINT_PERSON));
        if (coBasicContractCustRelation != null) {
            BasicCustBaseInfo cobasicCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>query().lambda()
                    .eq(BasicCustBaseInfo::getCustNo, coBasicContractCustRelation.getCustNo())
            );
            BasicCustAddressDetails coBasicCustAddressDetailsLiving = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>query().lambda()
                    .eq(BasicCustAddressDetails::getCustNo, coBasicContractCustRelation.getCustNo())
                    .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.LIVE)
            );
            BasicCustAddressDetails coBasicCustAddressDetailsLivingWork = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>query().lambda()
                    .eq(BasicCustAddressDetails::getCustNo, coBasicContractCustRelation.getCustNo())
                    .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.WORK)
            );
            if (cobasicCustBaseInfo != null) {
                coBasicMainCustVo.setCustName(cobasicCustBaseInfo.getCustName());
                coBasicMainCustVo.setCertNo(cobasicCustBaseInfo.getCertNo());
                coBasicMainCustVo.setTelPhone(cobasicCustBaseInfo.getPhone());
            }
//            if (coBasicCustAddressDetailsLiving != null) {
//                coBasicMainCustVo.setMailingAddress(coBasicCustAddressDetailsLiving.getAddress());
//            }
            if (coBasicCustAddressDetailsLivingWork != null) {
                coBasicMainCustVo.setMailingAddress(coBasicCustAddressDetailsLivingWork.getAddress());
            }

        }
        dowdTemVo.setCoCust(coBasicMainCustVo);
        //查询保证人信息
        BasicContractCustRelation bailBasicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>query().lambda()
                .eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.GUARANTEE_PERSON));
        if (bailBasicContractCustRelation != null) {
            BasicCustBaseInfo bailbasicCustBaseInfo = basicCustBaseInfoService.getOne(Wrappers.<BasicCustBaseInfo>query().lambda()

                    .eq(BasicCustBaseInfo::getCustNo, bailBasicContractCustRelation.getCustNo())
            );
            BasicCustAddressDetails bailBasicCustAddressDetailsWork = basicCustAddressDetailsService.getOne(Wrappers.<BasicCustAddressDetails>query().lambda()
                    .eq(BasicCustAddressDetails::getCustNo, bailBasicContractCustRelation.getCustNo())
                    .eq(BasicCustAddressDetails::getAddressType, AddressTypeEnum.WORK)
            );
            if (bailbasicCustBaseInfo != null) {
                bailBasicMainCustVo.setCertNo(bailbasicCustBaseInfo.getCertNo());
                bailBasicMainCustVo.setCustName(bailbasicCustBaseInfo.getCustName());
                bailBasicMainCustVo.setTelPhone(bailbasicCustBaseInfo.getPhone());
            }
//            if (bailBasicCustAddressDetailsLiving != null) {
//                bailBasicMainCustVo.setLivingAddress(bailBasicCustAddressDetailsLiving.getAddress());
//            }
            if (bailBasicCustAddressDetailsWork != null) {
                bailBasicMainCustVo.setMailingAddress(bailBasicCustAddressDetailsWork.getAddress());
            }

        }
        dowdTemVo.setBailCust(bailBasicMainCustVo);

        //查询渠道信息
        BasicChannelInfo basicChannelInfo = basicChannelInfoService.getOne(Wrappers.<BasicChannelInfo>query().lambda()
                .eq(BasicChannelInfo::getContractNo, contractNo)
        );
        if (basicChannelInfo != null) {
            basicChannelInfoVo.setChannelName(basicChannelInfo.getContactName());
            basicChannelInfoVo.setReceiptAccount(basicChannelInfo.getReceivingAccount());
            basicChannelInfoVo.setReceiptBankCode(basicChannelInfo.getReceivingBank());
        }
        dowdTemVo.setChannel(basicChannelInfoVo);
        //车款融资信息
        ContractUtils contractUtils = new ContractUtils();
        BasicFinancialAgreement carBasicFinancialAgreement = basicFinancialAgreementService.getOne(Wrappers.<BasicFinancialAgreement>query().lambda()
                .eq(BasicFinancialAgreement::getContractNo, contractNo)
                //车款
                .eq(BasicFinancialAgreement::getCostType, AssetTypeEnum.CAR_AMT)
        );
        if (carBasicFinancialAgreement != null) {
            carBasicFinancialAgreementVo.setCustRate(carBasicFinancialAgreement.getCustRate());
            carBasicFinancialAgreementVo.setLoanTerm(carBasicFinancialAgreement.getLoanTerm());
            carBasicFinancialAgreementVo.setSettleRate(carBasicFinancialAgreement.getSettleRate());

            dowdTemVo.setTailDownPayAmt(carBasicFinancialAgreement.getDownPayAmt());
            dowdTemVo.setTailMonthPayAmt(carBasicFinancialAgreement.getMonthPayAmt());
            dowdTemVo.setTailPayAmt(carBasicFinancialAgreement.getTailPayAmt());
            dowdTemVo.setTotalLoanAmt(carBasicFinancialAgreement.getLoanAmt());
            dowdTemVo.setTotalBigLoanAmt(contractUtils.numberToCNMontrayUnit(carBasicFinancialAgreement.getLoanAmt()));
            dowdTemVo.setAddAmt(carBasicFinancialAgreement.getAddAmt());
        }
        dowdTemVo.setCarCost(carBasicFinancialAgreementVo);
        //附加贷融资信息
        BasicFinancialAgreement addBasicFinancialAgreement = basicFinancialAgreementService.getOne(Wrappers.<BasicFinancialAgreement>query().lambda()
                .eq(BasicFinancialAgreement::getContractNo, contractNo)
                //附加贷
                .eq(BasicFinancialAgreement::getCostType, AssetTypeEnum.ADDITIONAL)
        );
        if (addBasicFinancialAgreement != null) {
            addBasicFinancialAgreementVo.setCustRate(addBasicFinancialAgreement.getCustRate());
            addBasicFinancialAgreementVo.setLoanTerm(addBasicFinancialAgreement.getLoanTerm());
            addBasicFinancialAgreementVo.setSettleRate(addBasicFinancialAgreement.getSettleRate());
            dowdTemVo.setTailDownPayAmt(addBasicFinancialAgreement.getDownPayAmt());
            dowdTemVo.setTailMonthPayAmt(addBasicFinancialAgreement.getMonthPayAmt());
            dowdTemVo.setTailPayAmt(addBasicFinancialAgreement.getTailPayAmt());
            dowdTemVo.setTotalLoanAmt(addBasicFinancialAgreement.getLoanAmt());
            dowdTemVo.setTotalBigLoanAmt(contractUtils.numberToCNMontrayUnit(addBasicFinancialAgreement.getLoanAmt()));
            dowdTemVo.setAddAmt(addBasicFinancialAgreement.getAddAmt());


        }
        dowdTemVo.setAddCost(addBasicFinancialAgreementVo);
        //查询增融项信息
        List<BasicFinancingItems> list = basicFinancingItemsService.list(Wrappers.<BasicFinancingItems>query().lambda()
                .eq(BasicFinancingItems::getContractNo, contractNo)
        );
        if (EmptyUtils.isNotEmpty(list)&&list.size()>0){
            for (BasicFinancingItems financingItems : list) {
                if (financingItems.getFinanceItemCode().equals(ContractFinancingItemsEnum.PURCHASE_TAX)){
                    isPurchaseTax = "■";
                    noPurchaseTax = "□";
                    purchaseTaxAmt = financingItems.getFinanceItemAmt() == null ? BigDecimal.ZERO : financingItems.getFinanceItemAmt();
                }else if (financingItems.getFinanceItemCode().equals(ContractFinancingItemsEnum.INSURANCE)) {
                    isInsurance = "■";
                    noInsurance = "□";
                    insuranceAmt = financingItems.getFinanceItemAmt() == null ? BigDecimal.ZERO : financingItems.getFinanceItemAmt();
                } else if (financingItems.getFinanceItemCode().equals(ContractFinancingItemsEnum.GPS)) {
                    isGps = "■";
                    noGps = "□";
                    gpsAmt = financingItems.getFinanceItemAmt() == null ? BigDecimal.ZERO : financingItems.getFinanceItemAmt();
                    gpsPrice = financingItems.getFinanceItemAmt() == null ? BigDecimal.ZERO : financingItems.getFinanceItemAmt();
                } else if (financingItems.getFinanceItemCode().equals(ContractFinancingItemsEnum.DECORATE)) {
                    isDecorate = "■";
                    noDecorate = "□";
                    decorateAmt = financingItems.getFinanceItemAmt() == null ? BigDecimal.ZERO : financingItems.getFinanceItemAmt();
                } else if (financingItems.getFinanceItemCode().equals(ContractFinancingItemsEnum.BOUTIQUE)) {
                    isBoutique = "■";
                    noBoutique = "□";
                    boutiqueAmt = financingItems.getFinanceItemAmt() == null ? BigDecimal.ZERO : financingItems.getFinanceItemAmt();
                }
            }
        }
        addPriceItems.setIsDecorate(isDecorate);
        addPriceItems.setNoDecorate(noDecorate);
        addPriceItems.setDecorateAmt(decorateAmt);
        addPriceItems.setIsGps(isGps);
        addPriceItems.setNoGps(noGps);
        addPriceItems.setGpsAmt(gpsAmt);
        addPriceItems.setIsInsurance(isInsurance);
        addPriceItems.setNoInsurance(noInsurance);
        addPriceItems.setInsuranceAmt(insuranceAmt);
        addPriceItems.setIsBoutique(isBoutique);
        addPriceItems.setNoBoutique(noBoutique);
        addPriceItems.setBoutiqueAmt(boutiqueAmt);
        addPriceItems.setIsPurchaseTax(isPurchaseTax);
        addPriceItems.setNoPurchaseTax(noPurchaseTax);
        addPriceItems.setPurchaseTaxAmt(purchaseTaxAmt);
        addPriceItems.setGpsPrice(gpsPrice);
//        addPriceItems.setDecoratePrice(decoratePrice);
//        addPriceItems.setBoutiquePrice(boutiquePrice);
        //查询银行卡信息
        BasicBankCard basicBankCard = basicBankCardService.getOne(Wrappers.<BasicBankCard>query().lambda()
                .eq(BasicBankCard::getContractNo, contractNo)
        );
        if (basicBankCard != null) {
            basicBankCardVo.setAccountName(basicBankCard.getAccountName());
            basicBankCardVo.setAccountNo(basicBankCard.getAccountNo());
            basicBankCardVo.setCertNo(basicBankCard.getCertNo());
            dowdTemVo.setBankCard(basicBankCardVo);
        }
        //查询挂靠信息
        BasicAffiliatedUnit basicAffiliatedUnit = basicAffiliatedUnitService.getOne(Wrappers.<BasicAffiliatedUnit>query().lambda()
                .eq(BasicAffiliatedUnit::getContractNo, contractNo)
        );
        if (basicAffiliatedUnit != null) {
            basicAffiliatedUnitVo.setAffiliatedName(basicAffiliatedUnit.getAffiliatedName());
            basicAffiliatedUnitVo.setRegistDetailAddress(basicAffiliatedUnit.getRegistAddress());

        }
        return IResponse.success(dowdTemVo);
    }

    //查询规则
    @ApiOperation("查询规则")
    @PostMapping("/contractQueryRule")
    public IResponse contractQueryRule(@RequestParam(value = "contractNo") String contractNo) {
        DowdTemRule dowdTemRule = new DowdTemRule();

        //是否是附加贷
        BasicProductParam addLoans = basicProductParamService.getOne(Wrappers.<BasicProductParam>lambdaQuery().
                eq(BasicProductParam::getContractNo, contractNo).eq(BasicProductParam::getAtomKey, AfsEnumUtil.key(ProductAtomEnum.IS_ADD_LOAN)));
        dowdTemRule.setAddLoans(EmptyUtils.isNotEmpty(addLoans) ? addLoans.getAtomValue() : null);
        //是否有保证人
        BasicContractCustRelation bailBasicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>query().lambda()
                .eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.GUARANTEE_PERSON));
        if (EmptyUtils.isEmpty(bailBasicContractCustRelation)) {
            dowdTemRule.setEnsure("no");
        } else {
            dowdTemRule.setEnsure("yes");
        }
        //是否有共借人
        BasicContractCustRelation coBasicContractCustRelation = basicContractCustRelationService.getOne(Wrappers.<BasicContractCustRelation>query().lambda()
                .eq(BasicContractCustRelation::getContractNo, contractNo)
                .eq(BasicContractCustRelation::getCustRole, PersonRoleTypeEnum.JOINT_PERSON));
        if (EmptyUtils.isEmpty(coBasicContractCustRelation)) {
            dowdTemRule.setEnsure("no");
        } else {
            dowdTemRule.setEnsure("yes");
        }
        //开票单位与合作商是否一致
        //合作商展业城市
        //上牌地城市
        //查询车辆信息
        BasicCarDetails basicCarDetails = basicCarDetailsService.getOne(Wrappers.<BasicCarDetails>query().lambda()
                .eq(BasicCarDetails::getContractNo, contractNo)
        );
        if (EmptyUtils.isNotEmpty(basicCarDetails)) {
            dowdTemRule.setLicenseCity(basicCarDetails.getLicenseCity());
            dowdTemRule.setLicenseProvince(basicCarDetails.getLicenseProvince());
        }
        //还款方式
        BasicProductParam repaymentType = basicProductParamService.getOne(Wrappers.<BasicProductParam>lambdaQuery().
                eq(BasicProductParam::getContractNo, contractNo).eq(BasicProductParam::getAtomKey, AfsEnumUtil.key(ProductAtomEnum.REPAYMENT_TYPE)));

        dowdTemRule.setRepayMode(EmptyUtils.isNotEmpty(repaymentType) ? repaymentType.getAtomValue() : null);
        //金融产品

        //合作商名称

        //挂靠方式
        List<BasicAffiliatedUnit> company = basicAffiliatedUnitService.list(Wrappers.<BasicAffiliatedUnit>query().lambda()
                .eq(BasicAffiliatedUnit::getContractNo, contractNo)
        );
        List companys = new ArrayList();

        if (EmptyUtils.isNotEmpty(company)) {
            for (BasicAffiliatedUnit basicAffiliatedUnit : company) {
                AffiliatedWayEnum affiliatedType = basicAffiliatedUnit.getAffiliatedType();
                companys.add(affiliatedType);
            }
            dowdTemRule.setAffiliatedWays(companys);
        }
        //车辆属性
        BasicProductParam carNature = basicProductParamService.getOne(Wrappers.<BasicProductParam>lambdaQuery().
                eq(BasicProductParam::getContractNo, contractNo).eq(BasicProductParam::getAtomKey, AfsEnumUtil.key(ProductAtomEnum.CAR_TYPE)));
        dowdTemRule.setCarNature(EmptyUtils.isNotEmpty(carNature) ? carNature.getAtomValue() : null);
        //车辆类型
        BasicProductParam carType = basicProductParamService.getOne(Wrappers.<BasicProductParam>lambdaQuery().
                eq(BasicProductParam::getContractNo, contractNo).eq(BasicProductParam::getAtomKey, AfsEnumUtil.key(ProductAtomEnum.CAR_TYPE)));
        dowdTemRule.setCarType(EmptyUtils.isNotEmpty(carType) ? carType.getAtomValue() : null);
        //业务类型
        BasicMainInfo basicMainInfo = basicMainInfoService.getOne(Wrappers.<BasicMainInfo>query().lambda()
                .eq(BasicMainInfo::getContractNo, contractNo)
        );
        if (EmptyUtils.isNotEmpty(basicMainInfo)) {
            dowdTemRule.setBusinessType(basicMainInfo.getBusinessType());
        }
        return IResponse.success(dowdTemRule);
    }

}
